﻿using System;
using System.Windows.Forms;
using System.Text;
using System.Drawing;

namespace HoldemView
{

    using HoldemGame;

    public static class CardFormatter
    {
        private static char[] suitChar = new char[]{
                '♣', '♦', '♥','♠'
                //c,d,h,s
            };

        public static void SetCards(RichTextBox rt, CardSet cards, int count, int visible, bool useIcons, bool italic)
        {
            //public static int Clubs       = 0; трефы  
            //public static int Diamonds    = 1; буби
            //public static int Hearts      = 2; черви
            //public static int Spades      = 3; пики

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < count; i++)
            {
                Card c = cards[i];

                if(i>=visible)
                    sb.Append("XX");
                else
                {

                    if(!Card.IsValidValue(c.Value))
                        sb.Append("??");
                    else
                    {
                       string sc = useIcons ? "" + suitChar[c.Suit] : Card.SuitChar(c.Suit);
                       sb.AppendFormat("{0}{1}", Card.RankChar(c.Rank), sc);
                    }
                }
                if (i < count - 1)
                    sb.Append(' ');
            }
            rt.Text = sb.ToString();
            Font italicFont = new Font(rt.Font, FontStyle.Italic);
            for (int i = 0; i < count; i++)
            {
                rt.Select(i * 3, 3);
                rt.SelectionFont = italic ? italicFont:rt.Font;
                if (i < visible)
                {
                    rt.SelectionColor = cards[i].IsBlack() ? Color.Black : Color.Red;
                }
                else
                {
                    rt.SelectionColor = Color.DarkGreen;
                }
            }

        }

        public static void UpdateText(RichTextBox rt, string text)
        {
            int pos = rt.SelectionStart;
            bool atEnd = pos>=rt.Text.Length;

            rt.Text = text;
            if(rt.Text.Length>=pos)
                rt.SelectionStart = pos;
            if (atEnd)
            {
                rt.SelectionStart = rt.Text.Length;
                rt.ScrollToCaret();
            }
        }

        public static void SetBkgr(Control ctrl, Color clr)
        {
            ctrl.BackColor = clr;
            foreach (Control child in ctrl.Controls)
            {
                if (!child.BackColor.Equals(SystemColors.Info))
                    SetBkgr(child, clr);
            }
        }
    }
}